package com.lyt.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.lyt.mall.order.vo.PayVO;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * @author: LiuYutong
 * 支付宝的配置文件
 */
@Component
@Data
public class AlipayTemplate {

    // 商户appid 沙箱账号:
    public static String APPID = "2021000121690732";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCSr5cu4RKV5L59CGHY3BcYnbmt9ZeW2FT/ScAWhyvVHFJ1z2UGNBFm0hG7yf7BHB2xZiyu9XG66wpCvgvZdNeQo3pgtz42ZBm1JS1z2BtHG/t6jRMoc3KDBc2rPLwt+DqEJw3HeTuB7AIuSp0EOQKJPgI9LeGHU3zqpOpbnpRO3icfUL9/7pMdyCp+78TPJ7b7HBpI0mRvQZRcNhe52A9h+RQ5KJO3SuhU2blYwQkF4ECMargpLSBexXZQorBtj0+fhbwNaCEdGpGvNmq7weemSsBKtnvqBIhS4Z4re/RW3cjDVam3kT7qZ96BPkTOHcODeuRf591uKi4EJ32M4jBvAgMBAAECggEAKTkzsbcLNBju99CNPxbLXt2kuyDUE06ExKIDW9FtHh0xh25bo9yqL01GMpJYCAlQxvRCBoFwDunTHmKujdRqaHmpANk+oKaNGv1xwqavM9LHX++ZvLqFKhFNdZxB/mFOgKlBFBgcAy1Ch6PscK1c6xkEJBFq2k86Qe9K9aihYXBb+ylpKuRsnHlZlAMNz0nrapdSBRtGbEVyq0AFpFpnv8FStXCsO4nI45UcF4orcZN+AaTITUhza41akBzMEbevvt3zGB4PCwKRebahQAjS+JlamTiWD/BB+mH280G19+QEvHoGL/4Ts0kJJKGVZngM2Jh2FtUE9jTuIG9Pm0SeEQKBgQDVjZSQytm6muctEQoJrGNAG4sQM/VZ9w9gj1hy9WQ2XaKj5GYQRqZiN/hoDvpT9HEkRUjHCJoEwcUisy5cP2bFiLXK/pK8jNB1dbvy3DvSg/VbG2YgvDVld9zEn0Gk3ple6c9TIA5raWiIIN1mHjd0wO5VK5j+aME8lvtNyEQppQKBgQCv14vrQ1WDS49Cr2kcbVsKiK9Nyy2qCp1aoCfTk1TLgSPIYysR0rBqdjKwfYi62pLkuEp5M7CXvDyJlIMWZ21uujDLI6qtdxvt9pfBQMNkl2PjvsR7axnqlOky/NT3aI+g6ZT6ogPtswZyri1p5zohfpAGVRhqf7oAAHybs/2NgwKBgFFvzFfrA1zF9aKZ/Nnzg2XPGxjyt323WYXZJEGykqSo0HzvfHx4dLq/3VrRW2TosrzCLx63iQSHYEX7TpKfGvH5UFo4+nwsttIUM7hJhAaji81ShX+2RiaLrwTMNpxIxvQK9aUddGJBsDs5TTTEESHU8Q5Jse5SxF32tZ7T4CqBAoGAb5LlRTJEYDrhe/6xQ0hgLB7unP3+y5ZQoeBdZPJnK3mVquDBw11cczDsmPAOggGOk3IcTyttMdpOzz1csCqQVWHu8/Z9pfOd1TBSulzNnlYxG6FAKs+aci5qid/DgfjmqcOMtDt5IqolvT6nsjr1t6sG1/3GsEy7SKUGbAWZLOECgYEAzqtlfydOs+QsG1vXip3dUzD+KwHtKjeqePFK5DyIFpLAphIEJjU9nXcu0e0Kz2FeTNE0zZwEgLWPebgbF9WBGI/xHP1iona5ztWlR4f+mkzWz9D7ciThe0YxSWEnkTugzW6mbBfwzU1IyOz0JcF3A8aAjAF/cTcmst5BySJv79s=";
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://order.lyt.com/payed/notify";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url = "http://order.lyt.com/orderPay/returnUrl";
    // 请求网关地址
    public static String URL = "https://openapi.alipaydev.com/gateway.do";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiwVBNBppyRQZqat43EUg0jydc8MLe4qnRlo4O+mTxSF5Wf+Rp98+uccIDCPX9dv9v/BOZFzLnNW0BBcGOieMs6abW6r8qLBDqspqiMVo9SDLaWHbYCqIFEWVBXxr4iWwGkE4htZTSqicGJJxpWNNL/SrxaOGv8y5KCDVE9VZDm6wySu3RXXBkVtYy1UTF2jZ7lKUaYplHff1eEaXkfNCCDWuQ2g3AKXKCP2DGbAqlT1Mu2hjV7DOPZqhV4FaTYAeEHp4+5jY+NBHfaGJtBSS98kpc4b+8/6ojPPeogRraS/FwaFJnoUHNIKn4KufN1eA5rzP026Dabr7UnCrGGtbQwIDAQAB";
    // 日志记录目录
    public static String log_path = "/log";
    // RSA2
    public static String SIGNTYPE = "RSA2";

    public String pay(PayVO payVo){
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL,
                APPID,
                RSA_PRIVATE_KEY,
                FORMAT,
                CHARSET,
                ALIPAY_PUBLIC_KEY,
                SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVo.getOut_trader_no());
        model.setSubject(payVo.getSubject());
        model.setTotalAmount(payVo.getTotal_amount());
        model.setBody(payVo.getBody());
        model.setTimeoutExpress("5000");
        model.setProductCode("11111");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
